/* Copyright 2002 Centrality Communicatoins, Inc. */ 

/*
This file defined some interfaces for NAVI Software usage. In this file, the following APIs were provided. 

1). Touch panel calibrations: TchCalibration(),
2). Brightness adaption :GetCurrentBright(),SetCurrentBright()
3). Display state: SetDisplayState();
4). System Information:GetSysInfo();
5). ROM Version: GetRomVersionInfo();
6). Set system sleep:SetSysSleep();
7). Turn on/off Backlight: TurnOnBacklight();
8). Set/Get the time to turn off Backlight: SetBacklight(),GetBacklight();
9). Volume tuning:VolumeTuning();
etc.. 

Created by QinghaiWang
qhwang@centralitycomm.com.cn 2005-04-07.
*/

//#include "windows.h"
//#include "pm.h"
//#include "BSPBKLIOCTL.H"
//#include "drvlib.h"
//#include "waveAC97.h"
//#include "Wavedev.h"
//#include "NaviAPI.h"
//#include "NAVIService.h"
//#include "MiscControl.h"
//#include <BspSpi.h>
//#include <spi.h>
//#include "glidebar.h"
//#include <Winnls.h>
//#include "securestoreapi.h"

#include "../stdafx.h"
#include "NaviAPI.h"

#include "../debug_new.h"

typedef DWORD (*PFN_SetSystemPowerState)(LPCWSTR, DWORD, DWORD);

#define DEF_AUDIO_LEVEL		0x1B		// default audio volume level
#define PWRMGR_API_REG_KEY      TEXT("SYSTEM\\CurrentControlSet\\Control\\Power\\Timeouts")

#define RV_BATTERYTIMEOUT            TEXT("BatteryTimeout")
#define RV_OLD_BATTERYTIMEOUT        TEXT("OldBatteryTimeout")
#define RV_USEBATTERY                TEXT("UseBattery")
#define RV_ACTIMEOUT                 TEXT("ACTimeout")
#define RV_OLD_ACTIMEOUT             TEXT("OldACTimeout")
#define RV_USEAC                     TEXT("UseExt")

#define EVENTNAME_BACKLIGHTCHANGEEVENT     TEXT("BackLightChangeEvent")

//
//BOOL AtlasNotifyRegister(HWND hWin,WORD Type, WORD MsgInfo, UINT WMNotify)
//{
//	BOOL bRet = FALSE;
//	HANDLE hDev;
//	REQUESTINFO Node;
//	
//	if ((WMNotify != 0)&& (hWin != NULL))
//	{
//		Node.hWin = hWin;
//		Node.Type = Type;
//		Node.MsgInfo = MsgInfo;		
//		Node.WMNotify = WMNotify;
//		hDev = CreateFile(TEXT("NAV1:"), 0,
//								  0,
//								NULL, 
//								OPEN_EXISTING,
//								0, 
//								0);
//		bRet = DeviceIoControl(hDev,IOCTL_NAVI_SERVICE_NOTIFY_REGISTER,&Node,sizeof(REQUESTINFO),&WMNotify,sizeof(UINT),NULL,NULL);
//		if(!bRet)
//			SetLastError(GetLastError());
//
//		CloseHandle(hDev);
//	}
//	return bRet;
//}
//BOOL    AtlasNotifyDeregister(HWND hWin,WORD Type, WORD MsgInfo)
//{
//	BOOL bRet = FALSE;
//	HANDLE hDev;
//	REQUESTINFO Node;
//	
//	if (hWin != NULL)
//	{
//		Node.hWin = hWin;
//		Node.Type = Type;
//		Node.MsgInfo = MsgInfo;		
//		hDev = CreateFile(TEXT("NAV1:"), 0,
//								  0,
//								NULL, 
//								OPEN_EXISTING,
//								0, 
//								0);
//		bRet = DeviceIoControl(hDev,IOCTL_NAVI_SERVICE_NOTIFY_DEREGISTER,&Node,sizeof(REQUESTINFO),NULL,0,NULL,NULL);
//		if(!bRet)
//			SetLastError(GetLastError());
//
//		CloseHandle(hDev);
//	}
//	return bRet;
//}
//BOOL    AtlasNotifyRespond(HWND hWin,WORD Type, WORD MsgInfo)
//{
//	BOOL bRet = FALSE;
//	HANDLE hDev;
//	REQUESTINFO Node;
//	
//	if (hWin != NULL)
//	{
//		Node.hWin = hWin;
//		Node.Type = Type;
//		Node.MsgInfo = MsgInfo;		
//		hDev = CreateFile(TEXT("NAV1:"), 0,
//								  0,
//								NULL, 
//								OPEN_EXISTING,
//								0, 
//								0);
//		bRet = DeviceIoControl(hDev,IOCTL_NAVI_SERVICE_NOTIFY_RESPOND, &Node,sizeof(REQUESTINFO),NULL,0,NULL,NULL);
//		if(!bRet)
//			SetLastError(GetLastError());
//
//		CloseHandle(hDev);
//	}
//	return bRet;
//}

/*******************************************************************************
Query light sensor state function
Return Value : 
********************************************************************************/
//BOOL AtlasGetLightLevel (PULONG pLevel)
//{
//	BOOL bRet = FALSE;
//	HANDLE hDev;
//
//	if (pLevel != NULL)
//	{
//		hDev = CreateFile(TEXT("MXC1:"), 
//								  0,
//								  0,
//								NULL, 
//								OPEN_EXISTING,
//								0, 
//								0);
//		bRet = DeviceIoControl(hDev,IOCTL_MISC_CONTROL_GET_LIGHTSENSOR, NULL,0, pLevel,sizeof(DWORD),NULL,NULL);
//		if(!bRet)
//			SetLastError(GetLastError());
//
//		CloseHandle(hDev);
//	}
//	return bRet;
//}

/*******************************************************************************
Query USB Mode function
Return Value : True, USB cable in cradle. else False
********************************************************************************/
BOOL AtlasGetUSBState(PULONG pState)
{
	BOOL bRet = FALSE;
	HANDLE hDev;

	if (pState != NULL)
	{
		hDev = CreateFile(TEXT("MXC1:"), 
			0,
			0,
			NULL, 
			OPEN_EXISTING,
			0, 
			0);
		bRet = DeviceIoControl(hDev,IOCTL_MISC_CONTROL_GET_USBSTATE, NULL,0, pState,sizeof(DWORD),NULL,NULL);
		if(!bRet)
			SetLastError(GetLastError());

		CloseHandle(hDev);
	}
	return bRet;
}



/********************************************************************************
Touchpnel calibrations
********************************************************************************/

BOOL AtlasTchCalibration()
{
	BOOL bRetTouch = FALSE;

#ifdef UNDER_CE
	//bRetTouch = TouchCalibrate();
	//if(!bRetTouch)
	//{
	//	SetLastError(GetLastError());
	//}
#endif

	return bRetTouch;
}

/********************************************************************************
**Brightness tuning ** 
GetCurrentBright()
SetCurrentBright()
********************************************************************************/
BOOL AtlasGetBKLightLevel(PULONG pBklGetLevel)
{
	BOOL bRetGetBright=FALSE;

#ifdef UNDER_CE

	HANDLE hDev;
	hDev = CreateFile(_T("BKL1:"), GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, 0);
	ULONG uLBright=0;
	bRetGetBright = DeviceIoControl(hDev, IOCTL_BKL_GET_INTENSITY, NULL, 0, &uLBright, sizeof(uLBright), NULL, NULL);
	if(!bRetGetBright)
		SetLastError(GetLastError());
	CloseHandle(hDev);
	//*pBklGetLevel = uLBright/6;
	*pBklGetLevel =  (uLBright-10)/5 ;
#endif

	return bRetGetBright;
}

BOOL AtlasSetBKLightLevel(PULONG pBklSetLevel)
{	
	BOOL bRetSetBright = TRUE;

#ifdef UNDER_CE

	HANDLE hDev;
	hDev = CreateFile(_T("BKL1:"), GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, 0);
	ULONG iBright = *pBklSetLevel;
	//iBright = iBright*2;
	//iBright = iBright*5;

	if(iBright == 10)
		iBright = 64;
	else
	{ 
		//iBright = iBright*6;
		//zxj update for blt
		iBright = (iBright)*5 + 10;
	}

	bRetSetBright = DeviceIoControl(hDev,IOCTL_BKL_SET_INTENSITY,&iBright,sizeof(iBright),NULL,0,NULL, NULL);
	CloseHandle(hDev);

#endif

	return bRetSetBright;
}


BOOL AtlasGetMaxBKLightLevel(PULONG pBklGetLevel)
{
	*pBklGetLevel = BKL_MAX_LEVEL;
	return TRUE;
#if 0
	BOOL bRetGetBright=0;
	HANDLE hDev;

	hDev = CreateFile(TEXT("bkl1:"), GENERIC_READ|GENERIC_WRITE,
		FILE_SHARE_READ|FILE_SHARE_WRITE,
		NULL, OPEN_ALWAYS, 0, 0);
	bRetGetBright	=	DeviceIoControl(hDev,IOCTL_BACKLIGHT_GETMAX,NULL,0,&(*pBklGetLevel),sizeof(CEDEVICE_POWER_STATE),NULL,NULL);
	if(!bRetGetBright)
		SetLastError(GetLastError());
	CloseHandle(hDev);
	return bRetGetBright;
#endif
}

BOOL AtlasSetMaxBKLightLevel(PULONG pBklSetLevel)
{
	return TRUE;
#if 0
	BOOL bRetSetBright=0;
	HANDLE hDev;
	if(*pBklSetLevel<BKL_MIN_LEVEL) *pBklSetLevel=BKL_MIN_LEVEL;
	if(*pBklSetLevel>BKL_MAX_LEVEL) *pBklSetLevel=BKL_MAX_LEVEL;  
	hDev = CreateFile(TEXT("bkl1:"), GENERIC_READ|GENERIC_WRITE,
		FILE_SHARE_READ|FILE_SHARE_WRITE,
		NULL, OPEN_ALWAYS, 0, 0);
	bRetSetBright	= DeviceIoControl(hDev, IOCTL_BACKLIGHT_SETMAX, pBklSetLevel, sizeof(CEDEVICE_POWER_STATE), NULL, 0, NULL, NULL);

	if(!bRetSetBright)
		SetLastError(GetLastError());

	/*	else
	{
	HKEY    hKey;
	if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("ControlPanel\\BackLight"), 0, 0, &hKey))
	{		
	RegSetValueEx(hKey, TEXT("BacklightLevel"), 0, REG_DWORD, (LPBYTE)pBklSetLevel, sizeof(DWORD));
	RegCloseKey(hKey);
	}	
	else

	bRetSetBright=0;	
	}
	*/	
	CloseHandle(hDev);

	return bRetSetBright;
#endif
}

/********************************************************************************
Set DisplayState
********************************************************************************/
//INT AtlasSetDisplayState(BOOL bOn)
//{
//	int iRet=-1;
//	CEDEVICE_POWER_STATE dps;
//	HDC hDisplayDC = NULL;
//
//	hDisplayDC = CreateDC(NULL,NULL,NULL,NULL);
//	if (bOn) // turn on
//        	dps = D0;
//	else if (bOn == 0)	// turn off
//        	dps = D4;
//    	else
//        	goto exit;
//
//	if (hDisplayDC)
//	{
//		iRet = ExtEscape(hDisplayDC,IOCTL_POWER_SET,0,NULL,sizeof(CEDEVICE_POWER_STATE),(LPSTR)&dps);
//	}
//	
//	if(iRet<0)
//		SetLastError(GetLastError());
//	CloseHandle(hDisplayDC);
//exit:	return iRet;
//}

/********************************************************************************
System Information 
********************************************************************************/
BOOL AtlasGetSysInfo(UINT uiParam,PVOID pvParam)
{
	BOOL bRetSysInfo=FALSE;
#ifdef UNDER_CE
	DWORD dwError=0;
	dwError = SystemParametersInfo(SPI_GETOEMINFO,uiParam,pvParam,0);
	if(!dwError)
		SetLastError(GetLastError());
	else
		bRetSysInfo=TRUE;
#endif
	return bRetSysInfo;
}

/********************************************************************************
System Sleep
********************************************************************************/
VOID AtlasSetSysSleep()
{
#ifdef UNDER_CE
	//PFN_SetSystemPowerState gpfnSetSystemPowerState		= NULL;
	//HMODULE hmCore;

	//gpfnSetSystemPowerState = NULL;
	//hmCore = (HMODULE) LoadLibrary(_T("coredll.dll"));

	//if(hmCore != NULL) 
	//{
	//	gpfnSetSystemPowerState = (PFN_SetSystemPowerState) GetProcAddress(hmCore, L"SetSystemPowerState");
	//	FreeLibrary(hmCore);
	//}

	//if(gpfnSetSystemPowerState != NULL)
	//{
	//	gpfnSetSystemPowerState(NULL, POWER_STATE_SUSPEND, POWER_FORCE);
	//}
	//else
	//{
	//	////RETAILMSG(1, (TEXT("++++++UTLNavi++++:Force enter sleep. PowerOffSystem...\r\n")));
	//	PowerOffSystem();
	//}
#endif
}

/********************************************************************************
Turn on/off backlight
********************************************************************************/ 
VOID AtlasTurnOnBacklight(BOOL bOn )
{	
	//	CEDEVICE_POWER_STATE dps;
	ULONG ulBkl;

	if (bOn ) // turn on
	{
		ulBkl = BKL_ON;
	}
	else	// turn off
	{
		ulBkl = BKL_OFF;
	}
	AtlasSetBKLightLevel(&ulBkl);

}

/********************************************************************************
**Get the time of system to enter UserIdle mode (will turn off backlight and LCD),
system power on Adapter** 
********************************************************************************/
BOOL AtlasGetACTimeOut(PULONG pACGetTime)
{
	BOOL	bRetGetACTimeout=FALSE;
	HKEY	hRegKey;
	unsigned long	status;
	DWORD	ValLen;
	DWORD	ValType;
	unsigned long	ACTimeout;


	status = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
		PWRMGR_API_REG_KEY, 0, 0, &hRegKey);

	if (ERROR_SUCCESS == status)
	{
		ValLen = sizeof(DWORD);

		if ((ERROR_SUCCESS != RegQueryValueEx ( hRegKey, TEXT("ACUserIdle"),
			NULL, &ValType,(PBYTE)&ACTimeout, &ValLen)) ) 
		{
			ACTimeout =0;
			bRetGetACTimeout=FALSE;
			////RETAILMSG(1, (TEXT("++++++UTLNavi++++: Failed getting ACTimeOut registry\r\n")));
		}
		else
		{
			bRetGetACTimeout=TRUE;
		}
		RegCloseKey(hRegKey);
	}
	else 
	{
		////RETAILMSG(1, (TEXT("++++++UTLNavi++++:Failed opening Registry Key\r\n")));
		ACTimeout = 0;
	}
	//			////RETAILMSG(1, (TEXT("+UTLNavi+:Getting ACTimeOut registry %d\r\n"),ACTimeout));

	*pACGetTime = ACTimeout;
	return bRetGetACTimeout;
}

/********************************************************************************
**Get the time of system to enter UserIdle mode (will turn off backlight and LCD),
system power on Battery** 
********************************************************************************/
BOOL AtlasGetBattTimeOut(PULONG pBattGetTime)
{
	BOOL	bRetGetBattTimeout=FALSE;
	HKEY	hRegKey;
	unsigned long	status;
	DWORD	ValLen;
	DWORD	ValType;
	unsigned long	BatteryTimeout;


	status = RegOpenKeyEx(HKEY_CURRENT_USER,
		TEXT("ControlPanel\\BackLight"), 0, 0, &hRegKey);

	if (ERROR_SUCCESS == status)
	{
		ValLen = sizeof(DWORD);

		if(ERROR_SUCCESS != RegQueryValueEx ( hRegKey,RV_BATTERYTIMEOUT,NULL, &ValType,(PBYTE)&BatteryTimeout, &ValLen) ) 
		{
			BatteryTimeout =0;
			bRetGetBattTimeout=FALSE;
			////RETAILMSG(1, (TEXT("++++++UTLNavi++++: Failed getting BatteryTimeout registry\r\n")));
		}
		else
			bRetGetBattTimeout=TRUE;
		RegCloseKey(hRegKey);
	}
	else 
	{
		////RETAILMSG(1, (TEXT("++++++UTLNavi++++:Failed opening Registry Key\r\n")));
		BatteryTimeout = 0;
	}
	//	////RETAILMSG(1, (TEXT("+UTLNavi+:Getting BatteryTimeout registry %d\r\n"),BatteryTimeout));

	*pBattGetTime = BatteryTimeout;
	return bRetGetBattTimeout;
}

/********************************************************************************
**Set the time of system to enter UserIdle mode (will turn off backlight and LCD),
system power on Adapter** 
pACSetTime in minutes
********************************************************************************/
BOOL AtlasSetACTimeOut(PULONG pACSetTime)
{

	HKEY    hKey;
	BOOL	bRetSetACTimeout=FALSE;
	//*pACSetTime=*pACSetTime*60;
	if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("ControlPanel\\BackLight"), 0, 0, &hKey))
	{	

		if(*pACSetTime > 0)
		{	
			DWORD dUse = 1;
			RegSetValueEx(hKey, RV_ACTIMEOUT, 0, REG_DWORD, (LPBYTE)&dUse, sizeof(DWORD));	
			RegSetValueEx(hKey, TEXT("ACUserIdle"), 0, REG_DWORD, (LPBYTE)pACSetTime, sizeof(DWORD));
		}
		else
		{	
			DWORD dUse = 0;
			RegDeleteValue(hKey,RV_ACTIMEOUT);
			RegSetValueEx(hKey, RV_USEAC, 0, REG_DWORD, (LPBYTE)&dUse, sizeof(DWORD));
			DWORD oldExternalTime = 60;
			RegSetValueEx(hKey, RV_OLD_ACTIMEOUT, 0, REG_DWORD, (LPBYTE)&oldExternalTime, sizeof(DWORD));
		}	
		RegCloseKey(hKey);
		bRetSetACTimeout=TRUE;
		HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, EVENTNAME_BACKLIGHTCHANGEEVENT);
		if (hEvent != NULL) 
		{
			SetEvent(hEvent);
			CloseHandle(hEvent);
		}
	}	
	return bRetSetACTimeout;
}

/********************************************************************************
**Set the time of system to enter UserIdle mode (will turn off backlight and LCD),
system power on battery** 
pBattSetTime in minutes

********************************************************************************/
BOOL AtlasSetBattTimeOut(PULONG pBattSetTime)
{

	HKEY    hKey;
	BOOL	bRetSetBattTimeout=FALSE;
	//*pBattSetTime=*pBattSetTime*60;

	if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("ControlPanel\\BackLight"), 0, 0, &hKey))
	{	
		if(*pBattSetTime > 0)
		{	
			DWORD dUse = 1;
			RegSetValueEx(hKey, RV_USEBATTERY, 0, REG_DWORD, (LPBYTE)&dUse, sizeof(DWORD));	
			RegSetValueEx(hKey, RV_BATTERYTIMEOUT, 0, REG_DWORD, (LPBYTE)pBattSetTime, sizeof(DWORD));
		}
		else
		{		
			DWORD dUse = 0;
			RegDeleteValue(hKey,RV_BATTERYTIMEOUT);
			RegSetValueEx(hKey, RV_USEBATTERY, 0, REG_DWORD, (LPBYTE)&dUse, sizeof(DWORD));
			DWORD oldBatteryTime = 15;
			RegSetValueEx(hKey, RV_OLD_BATTERYTIMEOUT, 0, REG_DWORD, (LPBYTE)&oldBatteryTime, sizeof(DWORD));
		}	
		RegCloseKey(hKey);
		bRetSetBattTimeout=TRUE;
		HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, EVENTNAME_BACKLIGHTCHANGEEVENT);
		if (hEvent != NULL) 
		{
			SetEvent(hEvent);
			CloseHandle(hEvent);
		}
	}	
	return bRetSetBattTimeout;
}

/********************************************************************************
Volume tuning
********************************************************************************/ 
BOOL AtlasSetAudioVolume(PULONG pLevel)      
{
	//MMDRV_MESSAGE_PARAMS InBuf;
	//DWORD OutBuf;
	//HANDLE hDev;
	//DWORD dwAudioLevel = *pLevel;

	//hDev = CreateFile(TEXT("WAV1:"), GENERIC_READ|GENERIC_WRITE,
	//							FILE_SHARE_READ|FILE_SHARE_WRITE,
	//							NULL, OPEN_ALWAYS, 0, 0);
	//		
	////set audio level
	//InBuf.uMsg= WODM_SET_AUDIOLEVEL;
	//InBuf.uDeviceId = 0;
	//InBuf.dwUser = 0;
	//InBuf.dwParam1 = dwAudioLevel;
	//InBuf.dwParam2 = 0;	
	//DeviceIoControl(hDev, IOCTL_WAV_MESSAGE, &InBuf, sizeof(InBuf), 
	//      				&OutBuf,sizeof(OutBuf), NULL, NULL);

	//
	////////RETAILMSG (APDEBUG, (TEXT("volume tunning set audio level  =%d \r\n"),OutBuf));
	//CloseHandle(hDev);

	return TRUE;
}

BOOL AtlasGetAudioVolume(PULONG pLevel)
{
	//	MMDRV_MESSAGE_PARAMS InBuf;
	//	DWORD OutBuf;
	//	HANDLE hDev;
	//	DWORD dwAudioLevel;
	//	
	//	hDev = CreateFile(TEXT("WAV1:"), GENERIC_READ|GENERIC_WRITE,
	//								FILE_SHARE_READ|FILE_SHARE_WRITE,
	//								NULL, OPEN_ALWAYS, 0, 0);
	//	// get current audio level
	//	InBuf.uMsg= WODM_GET_AUDIOLEVEL;
	//	InBuf.uDeviceId = 0;
	//	InBuf.dwUser = 0;
	//	InBuf.dwParam1 = 0;
	//	InBuf.dwParam2 = 0;
	//	DeviceIoControl(hDev, IOCTL_WAV_MESSAGE, &InBuf, sizeof(InBuf), 
	//       					&OutBuf,sizeof(OutBuf), NULL, NULL);
	//
	////	////RETAILMSG (APDEBUG, (TEXT("volume tunning audio level =%d \r\n"),OutBuf));
	//	dwAudioLevel = OutBuf;
	//	CloseHandle(hDev);
	//	*pLevel = dwAudioLevel;

	return TRUE;	
}

BOOL AtlasSetAudioMaxLevel(PULONG pLevel)      
{
	//MMDRV_MESSAGE_PARAMS InBuf;
	//DWORD OutBuf;
	//HANDLE hDev;
	//DWORD dwAudioLevel = *pLevel;
	//if (dwAudioLevel <= 0)
	//{
	//	dwAudioLevel = DEF_AUDIO_LEVEL;
	//}

	//hDev = CreateFile(TEXT("WAV1:"), GENERIC_READ|GENERIC_WRITE,
	//							FILE_SHARE_READ|FILE_SHARE_WRITE,
	//							NULL, OPEN_ALWAYS, 0, 0);
	//		
	////set audio level
	//InBuf.uMsg= WODM_SET_MAX_AUDIOLEVEL;
	//InBuf.uDeviceId = 0;
	//InBuf.dwUser = 0;
	//InBuf.dwParam1 = dwAudioLevel;
	//InBuf.dwParam2 = 0;	
	//DeviceIoControl(hDev, IOCTL_WAV_MESSAGE, &InBuf, sizeof(InBuf), 
	//      				&OutBuf,sizeof(OutBuf), NULL, NULL);
	//	
	////////RETAILMSG (APDEBUG, (TEXT("volume tunning set audio level  =%d \r\n"),OutBuf));
	//CloseHandle(hDev);

	return TRUE;

}

BOOL AtlasGetAudioMaxLevel(PULONG pLevel)
{
	//	MMDRV_MESSAGE_PARAMS InBuf;
	//	DWORD OutBuf;
	//	HANDLE hDev;
	//	DWORD dwAudioLevel;
	//	
	//	hDev = CreateFile(TEXT("WAV1:"), GENERIC_READ|GENERIC_WRITE,
	//								FILE_SHARE_READ|FILE_SHARE_WRITE,
	//								NULL, OPEN_ALWAYS, 0, 0);
	//	// get current audio level
	//	InBuf.uMsg= WODM_GET_MAX_AUDIOLEVEL;
	//	InBuf.uDeviceId = 0;
	//	InBuf.dwUser = 0;
	//	InBuf.dwParam1 = 0;
	//	InBuf.dwParam2 = 0;
	//	DeviceIoControl(hDev, IOCTL_WAV_MESSAGE, &InBuf, sizeof(InBuf), 
	//       					&OutBuf,sizeof(OutBuf), NULL, NULL);
	//
	////	////RETAILMSG (APDEBUG, (TEXT("volume tunning audio level =%d \r\n"),OutBuf));
	//	dwAudioLevel = OutBuf;
	//	CloseHandle(hDev);
	//	*pLevel = dwAudioLevel;

	return TRUE;	
}

DWORD AtlasVolumeTuning(BOOL bOn )
{
	//	HANDLE hAudioVolChangeEvent = NULL;
	//
	//	MMDRV_MESSAGE_PARAMS InBuf;
	//	DWORD OutBuf;
	//	HANDLE hDev;
	//	DWORD dwAudioLevel;	
	//	DWORD dwAudioMaxLevel;	
	//	hDev = CreateFile(TEXT("WAV1:"), GENERIC_READ|GENERIC_WRITE,
	//								FILE_SHARE_READ|FILE_SHARE_WRITE,
	//								NULL, OPEN_ALWAYS, 0, 0);
	//
	//	// get current audio level
	//	InBuf.uMsg= WODM_GET_MAX_AUDIOLEVEL;
	//	InBuf.uDeviceId = 0;
	//	InBuf.dwUser = 0;
	//	InBuf.dwParam1 = 0;
	//	InBuf.dwParam2 = 0;
	//	DeviceIoControl(hDev, IOCTL_WAV_MESSAGE, &InBuf, sizeof(InBuf), 
	//       					&OutBuf,sizeof(OutBuf), NULL, NULL);
	//
	//	dwAudioMaxLevel = OutBuf;
	//
	//	// get max audio level
	//	InBuf.uMsg= WODM_GET_AUDIOLEVEL;
	//	InBuf.uDeviceId = 0;
	//	InBuf.dwUser = 0;
	//	InBuf.dwParam1 = 0;
	//	InBuf.dwParam2 = 0;
	//	DeviceIoControl(hDev, IOCTL_WAV_MESSAGE, &InBuf, sizeof(InBuf), 
	//       					&OutBuf,sizeof(OutBuf), NULL, NULL);
	//
	////	////RETAILMSG (APDEBUG, (TEXT("volume tunning audio level =%d \r\n"),OutBuf));
	//	dwAudioLevel = OutBuf;
	//	
	//	//calc audio level
	//	if(!bOn)
	//	{	
	//		if (dwAudioLevel < dwAudioMaxLevel)
	//			 dwAudioLevel ++;
	//	}else
	//	{	
	//		if (dwAudioLevel >0)
	//			dwAudioLevel --;
	//	}
	//	//set audio level
	//	InBuf.uMsg= WODM_SET_AUDIOLEVEL;
	//	InBuf.uDeviceId = 0;
	//	InBuf.dwUser = 0;
	//	InBuf.dwParam1 = dwAudioLevel;
	//	InBuf.dwParam2 = 0;	
	//	DeviceIoControl(hDev, IOCTL_WAV_MESSAGE, &InBuf, sizeof(InBuf), 
	//       				&OutBuf,sizeof(OutBuf), NULL, NULL);
	//
	//	//////RETAILMSG (APDEBUG, (TEXT("volume tunning set audio level  =%d \r\n"),OutBuf));
	//	CloseHandle(hDev);
	//
	//	return dwAudioLevel;

#if 0	
	hAudioVolChangeEvent = CreateEvent(NULL, FALSE, FALSE, WAV_AUDIO_VOL_CHANGE);
	if (hAudioVolChangeEvent)
	{	
		if(bOn)
			SetEventData(hAudioVolChangeEvent, 1);	
		else
			SetEventData(hAudioVolChangeEvent, 0);	
		SetEvent(hAudioVolChangeEvent);
	}
	else
	{
		ERRORMSG(1, (TEXT("++++++UTLNavi++++:Create Audio Level Change Event failed!!!\r\n")));

	}

	CloseHandle(hAudioVolChangeEvent);
#endif

	return 0;
}

BOOL AtlasGetUUID(GUID * pUUID)
{
#ifdef UNDER_CE
	/*DWORD dwByteTransfered;
	if(!KernelIoControl (IOCTL_HAL_GET_UUID, 
		NULL, 
		0, 
		pUUID, 
		sizeof (GUID), 
		&dwByteTransfered))
	{
		////RETAILMSG(1,(TEXT("Get UUID KernelIoControl fail:%d"),GetLastError()));
		return FALSE;
	}*/
#endif
	return TRUE;
}

__inline HANDLE GetDeviceHandle()
{
	return CreateFile(
		TEXT( "DSK1:" ),
		GENERIC_READ | GENERIC_WRITE,
		FILE_SHARE_READ | FILE_SHARE_WRITE,
		NULL,
		OPEN_EXISTING,
		0,
		NULL
		);
}

//BOOL AtlasSetUUID(PVOID pUUID, DWORD dwBufSize )
//{
//    return SecureStoreSetUUID(pUUID, dwBufSize);
//}
//
//BOOL AtlasSetSecureInfo(PVOID pBuffer, DWORD dwBufSize )
//{
//    return SecureStoreSetSecureInfo(pBuffer, dwBufSize);
//   
//}
//
//BOOL AtlasGetSecureInfo(PVOID pBuffer, DWORD dwBufSize )
//{
//    return SecureStoreGetSecureInfo(pBuffer, dwBufSize);
//   
//}
//
//BOOL AtlasGetIRDAStatus(void)
//{
//	return TRUE;
//}
//
//
//BOOL AtlasTrunOnIRDA(BOOL bOn)
//{
//	return TRUE;
//
//}

#define VERSIONLEN 50
BOOL AtlasGetBSPVersion(TCHAR * pVerStr)
{
	HKEY    hKey;
	DWORD	ValType;
	DWORD	ValLen;

	if (pVerStr != NULL)
	{
		if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("ControlPanel\\VersionSetting"), 0, 0, &hKey))
		{	
			ValLen = VERSIONLEN;
			if (ERROR_SUCCESS != RegQueryValueEx(hKey, TEXT("BSPVserion"), 0, &ValType, (LPBYTE)pVerStr, &ValLen))
			{		
				return FALSE;		
			}
			RegCloseKey(hKey);		
		}

	}else
		return FALSE;

	return TRUE;
}

/************************************************************************/
/*This funcation for user calibrate glide sensor*/
/************************************************************************/
//BOOL AtlasCalibrateGlideSensor()
//{
//	 HANDLE  pHandle = NULL;
//	 BYTE   OutData;
//	 DWORD	ByteReturn;
//	 GLIDEBARCALIBRATE calibrateData;
//	 calibrateData.e_sampleRate = SampleRate_40;
//	 calibrateData.Xcheck = 0;
//	 calibrateData.Ycheck = 1;
//	 pHandle=CreateFile(TEXT("GLD1:"),GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,0);
//	 if(!pHandle)
//	{
//		////RETAILMSG(1,(TEXT("Open GLD1: device fail. Error= %d"),GetLastError()));
//		return FALSE;
//	}
//	DeviceIoControl(pHandle,IOCTL_CALIBRATE_GLIDEBAR,&calibrateData,sizeof(calibrateData),&OutData,sizeof(BYTE),&ByteReturn,NULL);
//	CloseHandle(pHandle);
//	if(OutData== ERROR_SUCCESS)
//	{
//		return TRUE;
//	}
//	else
//	{
//		return FALSE;
//	}
//}

/************************************************************************/
//By call this function you can attian the GPS current mode
// pState 
//           [out]:Pointer to a buffer that receives the operation's output data. 
//           "1" indicates GPS in Prediction Mode;"0" indicates GPS in Non-Prediction mode.
//Return Value: TRUE indicates success;FALSE indicates fail
/************************************************************************/
BOOL AtlasGetGPSMode (PULONG pState)
{
	BOOL bRet = FALSE;
	HANDLE hDev;

	if (pState != NULL)
	{
		hDev = CreateFile(TEXT("MXC1:"), 
			0,
			0,
			NULL, 
			OPEN_EXISTING,
			0, 
			0);
		bRet = DeviceIoControl(hDev,IOCTL_MISC_CONTROL_GET_GPSMODE, NULL,0, pState,sizeof(DWORD),NULL,NULL);
		if(!bRet)
			SetLastError(GetLastError());
		CloseHandle(hDev);
	}

	return bRet;
}